<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\AdRequest;
use App\Models\Ad;
use Auth;
use Validator;
use App\Models\Site;
class AdsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function create(Site $site, Request $request)
    {
      $result['status'] = 200;
      $result['message'] = '';
      if($site) {
        $this->authorize('destroy', $site);
        $initData = [
          'title' => '标题',
          'link' => 'https://www.baidu.com',
          'color' => 'rgb(194,204,162)',
          'border_color' => 'rgb(253,105,141)',
          'size' => 16,
          'flicker' => false,
          'user_id' => $site->user_id,
          'site_id' => $site->id,
          'show_border' => false
        ];
        $num = $request->input('num', 10);
        if ($num > 1) {
          for ($i = 0; $i < $num; $i++) {
            Ad::create($initData);
          }
          $result['message'] = '成功添加 ' . $num . '个广告！';
        } else {
          Ad::create($initData);
          $result['message'] = '成功添加一个广告！';
        }
      } else {
        $result['message'] = '站点不存在！';
        $result['status'] = 404;
      }
      return $result;
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Ad $ad, AdRequest $request)
    {
        $ad->fill($request->all());
        $ad->user_id = Auth::id();
        $ad->save();
        return redirect()->route('sites.edit', $request->site_id)->with('success', '添加成功！');
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Ad $ad, AdRequest $request)
    {
        $site_id = $ad->site_id;
        $ad->update($request->all());
        return redirect()->route('sites.edit', $ad->site_id)->with('success', '修改成功！');
    }

    public function update_all(Site $site, Ad $ad, Request $request)
    {
        // 进行权限验证
        $this->authorize('destroy', $site);
        $site_id = $ad->site_id;
        // $ad->update($request->all());
        $ads = $request->all();
        // 获取验证规则
        $requests = new AdRequest;
        $rules = $requests->rules();
        // 验证数据
        foreach ($ads as $key => $ad_data) {
            $ads[$key]['message'] = '';
            $validator = Validator::make($ad_data, $rules);
            if ($validator->fails()) {
                $ads[$key]['errors'] = $validator->errors();
            } else {
                if (isset($ad_data['id'])) {
                    $ad = Ad::find($ad_data['id']);
                    if($ad->update($ad_data)) {
                        $ads[$key] = $ad;
                        $ads[$key]['message'] = '更新成功';
                    }
                } else {
                    $ad_data['user_id'] = Auth::id();
                    $ad = Ad::create($ad_data);
                    $ads[$key] = $ad;
                    $ads[$key]['message'] = '添加成功';
                }

            }
        }
        return $ads;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Ad $ad)
    {
        $this->authorize('destroy', $ad);
        $ad->delete();
        return redirect()->route('sites.edit', $ad->site_id)->with('success', '删除成功！');
    }
}
